package app.modules.cloudpivot.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.engine.exceptions.DataSourceException;
import com.authine.cloudpivot.platform.notification.IMessageService;
import com.authine.cloudpivot.platform.notification.MessageDTO;
import com.authine.cloudpivot.web.api.service.EngineService;

/**
 * 自定义消息通道实现
 *
 * @author LiuGangQiang Create in 2024/02/28
 */
public class CustomMessageServiceImpl implements IMessageService {
    private static final Logger LOG = LoggerFactory.getLogger(CustomMessageServiceImpl.class);
    private final static String CHANNEL = "Custom";
    private static Map<String, String> config = new HashMap<>();

    @Override
    public String initChannel() {
        return CHANNEL;
    }

    /**
     * PostConstruct初始化避免每次都查询
     *
     * @author LiuGangQiang Create in 2025/08/03
     */
    @PostConstruct
    public void initConfig() {
        JSONObject jsonObject = JSONObject.parseObject(EngineService.getEngineService()
                .getPlatformConfigFacade()
                .getSystemSettingBySettingCode(CHANNEL)
                .getSettingValue());
        JSONArray parameterList = jsonObject.getJSONArray("parameterlist");
        config = parameterList.stream()
                .map(item -> (JSONObject) item)
                .collect(Collectors.toMap(item -> item.getString("code"), item -> item.getString("value")));
        LOG.info("===>>初始化消息通道 通道编码：{} 配置信息：{}", CHANNEL, config);
    }

    @Override
    public List<String> sendMsg(MessageDTO message) throws DataSourceException {
        /* TODO 发送消息逻辑 */
        return null;
    }
}
